package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.AdActivePersonnelMapper;
import com.zhentao.model.activity.dto.ActivityDto;
import com.zhentao.model.activity.dto.JoinDto;
import com.zhentao.model.activity.pojo.AdActivePersonnel;
import com.zhentao.model.activity.pojo.AdActivity;
import com.zhentao.model.banner.pojo.AdBanner;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.realname.pojo.ApRealname;
import com.zhentao.model.social.dto.SocialLikeDto;
import com.zhentao.model.social.pojo.ApUserLike;
import com.zhentao.service.AdActivityService;
import com.zhentao.mapper.AdActivityMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ad_activity】的数据库操作Service实现
* @createDate 2024-07-02 18:56:15
*/
@Service
public class AdActivityServiceImpl extends ServiceImpl<AdActivityMapper, AdActivity>
    implements AdActivityService{
    @Resource
    AdActivityMapper adActivityMapper;
    @Resource
    AdActivePersonnelMapper adActivePersonnelMapper;

    @Override
    public ResponseResult getList(ActivityDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<AdActivity> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<AdActivity> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(dto.getTitle()!=null,AdActivity::getTitle,dto.getTitle());
        page = adActivityMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    @Override
    public ResponseResult add(AdActivity activity) {
        if (activity==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        activity.setStatus(0);
        adActivityMapper.insert(activity);
        return ResponseResult.okResult("活动添加成功");
    }

    @Override
    public ResponseResult updateActivity(AdActivity activity) {
        if (activity==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        adActivityMapper.updateById(activity);
        return ResponseResult.okResult("活动修改成功");
    }

    @Override
    public ResponseResult putway(int id) {
        AdActivity adActivity = adActivityMapper.selectById(id);
        if (adActivity==null){
            return ResponseResult.errorResult(400,"未找到该活动");
        }
        adActivity.setStatus(adActivity.getStatus()==0 ? 1:0);
        if (adActivity.getStatus()==1){
            adActivity.setPublishTime(new Date());
        }
        adActivityMapper.updateById(adActivity);
        return ResponseResult.okResult("状态修改成功");
    }

    @Override
    public ResponseResult appList(ActivityDto dto) {
        Integer pageSize = dto.getPageSize();
        if (ObjectUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        dto.setPageSize(pageSize);
        List<JoinDto> list=adActivityMapper.selectListJoin(dto);
        for (JoinDto joinDto:list){
            QueryWrapper<AdActivePersonnel> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(AdActivePersonnel::getActivityId,joinDto.getId());
            wrapper.lambda().eq(AdActivePersonnel::getUserId,dto.getUserId());
            AdActivePersonnel adActivePersonnel = adActivePersonnelMapper.selectOne(wrapper);
            if (adActivePersonnel!=null){
                joinDto.setJoinStatus(1);
            }else {
                joinDto.setJoinStatus(0);
            }

        }

        return  ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult selectOne(ActivityDto dto) {
            JoinDto joinDto=adActivityMapper.selectJoin(dto.getId(),dto.getUserId());
            if(ObjectUtils.isEmpty(joinDto)){
                JoinDto joinDto1 =adActivityMapper.selectOneAc(dto.getId());
                joinDto1.setJoinStatus(0);
                return ResponseResult.okResult(joinDto1);
            }
            QueryWrapper<AdActivePersonnel> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(AdActivePersonnel::getActivityId,joinDto.getId());
            wrapper.lambda().eq(AdActivePersonnel::getUserId,dto.getUserId());
            AdActivePersonnel adActivePersonnel = adActivePersonnelMapper.selectOne(wrapper);
            if (adActivePersonnel!=null){
                joinDto.setJoinStatus(1);
            }else {
                joinDto.setJoinStatus(0);
            }
        return  ResponseResult.okResult(joinDto);
    }

}




